package com.clz.video.mobilevideo;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.clz.appdesign.appdependence.base.BaseActivity;
import com.clz.video.mobilevideo.fragment.LocalMusicFragment;
import com.clz.video.mobilevideo.fragment.LocalVideoFragment;
import com.clz.video.mobilevideo.fragment.NetMusicFragment;
import com.clz.video.mobilevideo.fragment.NetVideoFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {


    @BindView(R.id.rb_local_video)
    RadioButton mRbLocalVideo;
    @BindView(R.id.rg_tab)
    RadioGroup mRgTab;
    @BindView(R.id.fl_content)
    FrameLayout mFlContent;
    private LocalVideoFragment mLocalVideoFragment;
    private LocalMusicFragment mLocalMusicFragment;
    private NetVideoFragment mNetVideoFragment;
    private NetMusicFragment mNetMusicFragment;
    private FragmentManager mFm;
    private FragmentTransaction mTransaction;

    @Override
    public void initToolbar() {

    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        mRbLocalVideo.setChecked(true);
    }

    @Override
    public void initData() {
        mFm = getFragmentManager();
        mLocalVideoFragment = new LocalVideoFragment();
        mTransaction = mFm.beginTransaction();
        mTransaction.add(R.id.fl_content, mLocalVideoFragment).commit();
    }

    @Override
    public void doBusiness(Context mContext) {
    }

    @Override
    public int layoutResID() {
        return R.layout.activity_main;
    }


    @OnClick({R.id.rb_local_video, R.id.rb_local_music, R.id.rb_net_video, R.id.rb_net_music})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rb_local_video:
                showFragment(0);
                break;
            case R.id.rb_local_music:
                showFragment(1);
                break;
            case R.id.rb_net_video:
                showFragment(2);
                break;
            case R.id.rb_net_music:
                showFragment(3);
                break;
            default:
                break;
        }
    }

    private void showFragment(int type) {
        mTransaction = mFm.beginTransaction();
        hideFragment(mTransaction);
        switch (type) {
            case 0:
                if (mLocalVideoFragment == null) {
                    mLocalVideoFragment = new LocalVideoFragment();
                    mTransaction.add(R.id.fl_content, mLocalVideoFragment);
                } else {
                    mTransaction.show(mLocalVideoFragment);
                }
                break;
            case 1:
                if (mLocalMusicFragment == null) {
                    mLocalMusicFragment = new LocalMusicFragment();
                    mTransaction.add(R.id.fl_content, mLocalMusicFragment);
                } else {
                    mTransaction.show(mLocalMusicFragment);
                }
                break;
            case 2:
                if (mNetVideoFragment == null) {
                    mNetVideoFragment = new NetVideoFragment();
                    mTransaction.add(R.id.fl_content, mNetVideoFragment);
                } else {
                    mTransaction.show(mNetVideoFragment);
                }
                break;
            case 3:
                if (mNetMusicFragment == null) {
                    mNetMusicFragment = new NetMusicFragment();
                    mTransaction.add(R.id.fl_content, mNetMusicFragment);
                } else {
                    mTransaction.show(mNetMusicFragment);
                }
                break;
        }
        mTransaction.commit();
    }

    //隐藏所有的fragment
    private void hideFragment(FragmentTransaction transaction) {
        if (mLocalMusicFragment != null) {
            transaction.hide(mLocalMusicFragment);
        }
        if (mLocalVideoFragment != null) {
            transaction.hide(mLocalVideoFragment);
        }
        if (mNetMusicFragment != null) {
            transaction.hide(mNetMusicFragment);
        }
        if (mNetVideoFragment != null) {
            transaction.hide(mNetVideoFragment);
        }
    }
}
